﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess.Sql
{
    /// <summary>
    /// SQL表达式，用以构造 left = right 样式的表达式
    /// </summary>
    public class SqlExpression : SqlPiece
    {
        /// <summary>
        /// SQL表达式
        /// </summary>
        internal SqlExpression()
        {
        }

        /// <summary>
        /// SQL表达式
        /// </summary>
        /// <param name="left">表达式的左部</param>
        /// <param name="right">表达式的右部</param>
        public SqlExpression(string left, string right)
        {
            if (right == null)
            {
                BuildContent(left, "NULL");
            }
            else if (right == string.Empty)
            {
                BuildContent(left, "''");
            }
            else
            {
                BuildContent(left, SqlStringUtils.QuotedString(right));
            }
        }

        /// <summary>
        /// SQL表达式
        /// </summary>
        /// <param name="left">表达式的左部</param>
        /// <param name="right">表达式的右部</param>
        public SqlExpression(string left, int right)
        {
            BuildContent(left, right.ToString());
        }

        /// <summary>
        /// SQL表达式
        /// </summary>
        /// <param name="left">表达式的左部</param>
        /// <param name="right">表达式的右部</param>
        public SqlExpression(string left, bool right)
        {
            BuildContent(left, right ? "1" : "0");
        }

        /// <summary>
        /// 转换 GUID 的SQL表达式。如果 GUID 为空(Guid.Empty)，则视作 NULL
        /// </summary>
        /// <param name="left">表达式的左部</param>
        /// <param name="right">表达式的右部</param>
        public SqlExpression(string left, Guid right)
            : this(left, right, true)
        {
        }

        /// <summary>
        /// 转换 GUID 的SQL表达式。如果 GUID 为空，则视作 NULL
        /// </summary>
        /// <param name="left">表达式的左部</param>
        /// <param name="right">表达式的右部</param>
        /// <param name="treatEmptyAsNull">是否将空(Guid.Empty)视作 NULL</param>
        public SqlExpression(string left, Guid right, bool treatEmptyAsNull)
        {
            string svalue;
            if (right == Guid.Empty)
            {
                svalue = "NULL";
            }
            else
            {
                svalue = SqlStringUtils.QuotedString(right.ToString());
            }

            BuildContent(left, svalue);
        }

        /// <summary>
        /// 构造SQL表达式
        /// </summary>
        /// <param name="left">表达式的左部</param>
        /// <param name="right">表达式的右部</param>
        void BuildContent(string left, string right)
        {
            Content.Append(left);
            Content.Append(" = ");
            Content.Append(right);
        }

        /// <summary>
        /// 输出转换后的SQL表达式
        /// </summary>
        /// <returns>转换后的SQL语句</returns>
        public override string ToString()
        {
            return Content.ToString();
        }
    }
}
